namespace DependencyManagement{

    /**
     *
     *注入到服务
     * @export
     * @template T
     * @param {string} key
     * @returns {Function}
     */
    export function Service<T>(key:string):Function{
        return function(target:T){
            getInstance().setContainer(key,target);
        }
    }


    /**
     *
     *获取容器
     * @export
     * @template T
     * @param {string} key
     * @returns
     */
    export function GetContainer<T>(key:string):T{
        return getInstance().getContainer<T>(key);
    }
}


/**
 *容器
 *
 * @class Container
 */
class Container{
    public constructor(){

    }

    private store:Object={};


    /**
     *设置容器
     *
     * @param {string} key
     * @param {Object} value
     * @memberof Container
     */
    public setContainer(key:string,value:Object){
        this.store[key]=value;
    }


    /**
     *
     *获取容器
     * @template T
     * @param {string} key
     * @returns {T}
     * @memberof Container
     */
    public getContainer<T>(key:string):T{
        let obj=this.store[key];
        if(!obj){
            throw new Error('没有获取到相关的对象');
        }
        return new obj();
    }
}

let container=null;

function getInstance():Container{
    if(container==null){
        container=new Container();
        return container;
    }

    return container;
}